package com.wt.springboot.push;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import com.google.gson.JsonObject;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IDEA
 * Describe:
 * Author:TianJi
 * Date:2018/11/19
 * Time:13:50
 */
public class PushDemo {

    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void JPush(String message, String deviceId) {
        // 设置好账号的app_key和masterSecret
        String appKey = "e6b579f3214938f121468cbd";
        String masterSecret = "f7c6f288292fd8836b8cbdb3";
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);

        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
                .setAudience(Audience.alias(deviceId))
                .setMessage(Message.content(message)).build();

        try {
            System.out.println(sdf.format(new Date()) + "极光推送参数:" + message + "deviceId:" + deviceId + "--payload:" + payload);
            payload.resetOptionsTimeToLive(60);
            //jpushClient.sendAndroidMessageWithAlias("message", message, new String[] { deviceId });
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("极光推送结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void JPush2(String message, String deviceId) {
        // 设置好账号的app_key和masterSecret
        String appKey = "e6b579f3214938f121468cbd";
        String masterSecret = "f7c6f288292fd8836b8cbdb3";
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);

        PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
                .setAudience(Audience.alias(deviceId))
                .setMessage(Message.newBuilder().setMsgContent(message).build()).build();

        try {
            System.out.println(sdf.format(new Date()) + "极光推送参数:" + message + "deviceId:" + deviceId + "--payload:" + payload);
            payload.resetOptionsTimeToLive(60);
            //PushResult result = jpushClient.sendAndroidMessageWithAlias("message", message, new String[]{deviceId});
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("极光推送结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JsonObject json = new JsonObject();
        json.addProperty("hashValue", "7d9bc6436f9e4743aec5f0991ca3a847");
        json.addProperty("type", "open");
        json.addProperty("wxId", "orGj9sxljFZS8K6mRhEPQqJmUL_4");
        json.addProperty("lockerNum", "3");
        json.addProperty("lockerName", "3");
        json.addProperty("message", "");
        json.addProperty("optionType", "save");
        json.addProperty("deviceId", "758373327");
        String msg = json.toString();
        //GePush("个推消息:他只是一个好人啊","788123725");
        JPush2(msg,"758373327");
        //JPush("极光消息:他只是一个好人啊","194123505");
    }

}
